package id3.utils;

import id3.controllers.ContinuousToDiscrete;
import id3.models.RowData;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Random;
import javax.swing.JOptionPane;

/**
 *
 * @author NguyenVan
 */
public class RandomData {

    public boolean randomData(String dataFile, String trainingFile, String testFile, int rateTraining) {
        try {
            ArrayList<RowData> datas = LoadFile.getData(dataFile);
            //datas=ContinuousToDiscrete.updateData(datas);
            int[] counts = {0, 0};
            for (RowData rowData : datas) {
                if (rowData.getRow().get(Constants.NUMBER_ATTRIBUTE - 1).compareToIgnoreCase(Constants.LABEL[0]) == 0) {
                    counts[0]++;
                } else {
                    counts[1]++;
                }
            }
            datas.sort(new Comparator<RowData>() {

                @Override
                public int compare(RowData o1, RowData o2) {
                    Random r = new Random(System.currentTimeMillis());
                    return r.nextInt(2) - 1;
                }
            });
            ArrayList<RowData> datasTraining = getData(datas, counts[0] * rateTraining / 10, Constants.LABEL[0]);
            datasTraining.addAll(getData(datas, counts[1] * rateTraining / 10, Constants.LABEL[1]));
            ArrayList<RowData> datasTest = getData(datas, counts[0] * (10 - rateTraining) / 10, Constants.LABEL[0]);
            datasTest.addAll(getData(datas, counts[1] * (10 - rateTraining) / 10, Constants.LABEL[1]));
            WriteFile(datasTraining, trainingFile);
            WriteFile(datasTest, testFile);
            return true;
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.toString());
            return false;
        }
    }

    private boolean WriteFile(ArrayList<RowData> rowDatas, String fileName) {
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
            for (RowData rowData : rowDatas) {
                String join = String.join(",", rowData.getRow());
                writer.write(join);
                writer.newLine();
            }
            writer.close();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    private ArrayList<RowData> getData(ArrayList<RowData> rowDatas, int count, String label) {
        try {
            ArrayList<RowData> rowDatas1 = new ArrayList<>();
            int i = 0;
            while (count >= 0 && i < rowDatas.size()) {
                count--;
                while (i < rowDatas.size() && rowDatas.get(i).getRow().get(Constants.NUMBER_ATTRIBUTE - 1).compareToIgnoreCase(label) != 0) {
                    i++;
                }
                if (i < rowDatas.size()) {
                    rowDatas1.add(rowDatas.get(i));
                    rowDatas.remove(i);
                }
            }
            return rowDatas1;
        } catch (Exception e) {
            return null;
        }
    }
}
